﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using COFE.Core.Logger;
using COFE.Core;

namespace COFE.Data
{
    public class DBFileSystemEventLogger : 
        DBEventLoggerBase<COFE.Data.FileSystemEvent, FileSystemEventArgsExA>

    {
        #region Methods        

        public override void DispatchEvent(object sender, EventArgs args)
        {
            if (args is FileSystemEventArgsExA)
                DispatchEvent(sender, (FileSystemEventArgsExA)args);
        }

        public override void DispatchEvent(object sender, FileSystemEventArgsExA args)
        {            
            #region Get Entry Ids
            Guid entryId = Guid.Empty, orgEntryId = Guid.Empty;
            string primVolId, parsePath, orgVolId = null, orgParsePath = null;
            COFEPath.ParseFullParseName(args.ParsePath, out primVolId, out parsePath);
            if (primVolId == null)
                throw new ArgumentException("ParsePath - no volumeid.");

            if (args is RenameEventArgsExA)
            {
                RenameEventArgsExA rArgs = args as RenameEventArgsExA;
                COFEPath.ParseFullParseName(args.ParsePath, out orgVolId, out orgParsePath);
            }

            COFEDBShared.DBOperations.ReadDatabase(
                (context) =>
                {
                    var entry = context.Entries.FirstOrDefault(
                        e => e.ParsePath == parsePath &&
                        e.PrimaryVolumeId == primVolId);
                    if (entry != null)
                        entryId = entry.EntryId;

                    if (args is RenameEventArgsExA)
                    {
                        var orgEntry = context.Entries.FirstOrDefault(
                        e => e.ParsePath == orgParsePath &&
                        e.PrimaryVolumeId == orgVolId);
                        if (orgEntry != null)
                            orgEntryId = orgEntry.EntryId;
                    }

                });
            #endregion

            string userId =
                        COFEShared.CurrentUserProfile == null ?
                        COFEShared.AdminUserProfile.UserName :
                        COFEShared.CurrentUserProfile.UserName;

            COFEDBShared.DBOperations.UpdateDatabase(
               (context) =>
               {
                   var fileSystemEvent = new COFE.Data.FileSystemEvent()
                   {
                       UserId = userId,
                       Sender = sender == null ? "" : sender.ToString(),
                       Time = DateTime.UtcNow,
                       Type = (int)args.ChangeType,
                       EntryId = entryId,
                       ParsePath = parsePath,
                       PrimaryVolumeId = primVolId,
                       IsFolder = args.IsFolder,
                       Message = args.ToString()                 
                   };

                   if (orgParsePath != null)
                       fileSystemEvent.OrginalParsePath = orgParsePath;
                   if (orgVolId != null)
                       fileSystemEvent.OrginalPrimaryVolumeId = orgVolId;                   

                   context.Events.AddObject(fileSystemEvent);
                   context.SaveChanges(); 
               });
        }

        protected override IEventLog createEventArgs(COFE.Data.FileSystemEvent e)
        {
            FileSystemEventArgsExA args;
            ModifiedAction ma = (ModifiedAction)e.Type;
            string parsePath = COFEPath.FormatFullParseName(e.PrimaryVolumeId, e.ParsePath);
            switch (ma)
            {
                case ModifiedAction.Renamed : 
                    string orgParsePath = COFEPath.FormatFullParseName(
                        e.OrginalPrimaryVolumeId, e.OrginalParsePath);
                    args = new RenameEventArgsExA(e.PrimaryVolumeId, ma,
                        e.IsFolder, parsePath, orgParsePath);
                    break;
                default :
                    args = new FileSystemEventArgsExA(e.PrimaryVolumeId, ma,
                        e.IsFolder, parsePath);
                    break;
            }            
            return new EventLog<FileSystemEventArgsExA>(e.EventId, e.Time, 
                e.Sender, e.UserId, args);                                       
        }

       


        #endregion
    }
}
